#include "BasePlainTextEdit.h"


namespace UserManagementSystem {
BasePlainTextEdit::BasePlainTextEdit(QWidget* parent)
    :QWidget(parent)
{
    this->setFixedSize(490,120);

    m_mainLayout=new QVBoxLayout(this);
    m_mainLayout->setContentsMargins(0,0,0,0);

    m_plainTextEdit=new QPlainTextEdit(this);

    m_textCntLabel=new QLabel(this);
    m_textCntLabel->setFixedSize(50,20);

    m_mainLayout->addWidget(m_plainTextEdit);

    m_textCntLabel->move(this->rect().right()-50-5,this->rect().bottom()-20);


    SetDefaultStyle();

    connect(m_plainTextEdit,&QPlainTextEdit::textChanged,this,&BasePlainTextEdit::On_Change_Label);
}

void BasePlainTextEdit::On_Change_Label()
{
    qDebug()<<this->rect();

    int text_cnt=m_plainTextEdit->toPlainText().size();
    if(text_cnt>500){
        text_cnt=500;
        m_plainTextEdit->setReadOnly(true);
    }else{
        if(m_plainTextEdit->isReadOnly()){
            m_plainTextEdit->setReadOnly(false);
        }
    }
    m_textCntLabel->setText(QString("%1/500").arg(text_cnt));
}

void BasePlainTextEdit::SetDefaultStyle()
{
    this->setStyleSheet(R"(
    QWidget{
            background-color: #f7f9fb;
            border: 1px solid #f7f9fb;
            border-radius: 4px;
            color:#d9dad4;
    }
    QWidget::hover{
            border: 1px solid #4672C4;
    }

    )");

    m_textCntLabel->setStyleSheet(R"(
        background-color: transparent;
        color:gray;

    )");
    m_plainTextEdit->setStyleSheet(R"(
        padding:15px;
        color:gray;

    )");

    this->m_plainTextEdit->setPlaceholderText(tr("请输入..."));

    this->m_textCntLabel->setText(tr("0/500"));
}

}
